package org.goldfish.message_board01.servlets;

import jakarta.servlet.*;
import jakarta.servlet.http.*;
import java.io.IOException;
import java.util.List;

import org.goldfish.message_board01.filters.CustomAuthFilter;
import org.goldfish.message_board01.services.MessageService;
import org.goldfish.message_board01.entity.MessagePreview;

public class MessageListServlet extends GFAbstractServlet {
    private final MessageService messageService = new MessageService();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession(false);

        List<MessagePreview> messageList = messageService.getAllMessagePreviews();

        try {
            List<String> onlineUsers=(List<String>) request.getServletContext().getAttribute(CustomAuthFilter.ONLINE_USERS);
            messageList.forEach(bean -> {
                String sender = bean.getSender();
                request.setAttribute(sender, onlineUsers.contains(sender));
            });
        }catch (Exception ignored){
        }
        request.setAttribute("messageList", messageList);

        // Forward to JSP
        request.getRequestDispatcher("messageList.jsp").forward(request, response);
    }

}
